﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class CustomerDataAccess
    {
        public int CustomerRegister(CustomerInfo customer)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.Account.Equals(customer.Account)).SingleOrDefault();
            var custPersonalIdentity = db.Customers.Where(cp => cp.PersonalIdentity.Equals(customer.PersonalIdentity)).SingleOrDefault();
            var custEmail = db.Customers.Where(cp => cp.Email.Equals(customer.Email)).SingleOrDefault();
            if (cust == null)
            {
                Customer c = new Customer();
                c.Firstname = customer.Firstname;
                c.Lastname = customer.Lastname;


                if (custPersonalIdentity == null)
                {
                    c.PersonalIdentity = customer.PersonalIdentity;
                }
                else
                {
                    return 4; // personal identity number is duplicate
                }

                c.Account = customer.Account;
                c.Password = customer.Password;
                c.Job = customer.Job;
                c.Dob = customer.Dob;
                c.Address = customer.Address;
                c.Phone = customer.Phone;
                if (custEmail == null)
                {
                    c.Email = customer.Email;
                }
                else
                {
                    return 5; //email is duplacate
                }

                try
                {
                    db.Customers.InsertOnSubmit(c);
                    db.SubmitChanges();
                    return 1; // register successfull
                }
                catch (Exception ex)
                {
                    return 2; // register fail
                }

            }
            else
            {
                return 3; // account is exist
            }
        }

        public CustomerInfo CustomerLogin(string account, string password)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.Account.Equals(account) && c.Password.Equals(password)).SingleOrDefault();
            if (cust != null) //found
            {
                CustomerInfo c = new CustomerInfo();
                c.CustomerID = cust.CustomerID;
                c.Firstname = cust.Firstname;
                c.Lastname = cust.Lastname;
                c.PersonalIdentity = cust.PersonalIdentity;
                c.Account = cust.Account;
                c.Password = cust.Password;
                c.Job = cust.Job;
                c.Dob = cust.Dob;
                c.Address = cust.Address;
                c.Phone = cust.Phone;
                c.Email = cust.Email;

                return c;
            }
            else // not found
            {
                return null;
            }
        }

        public CustomerInfo getCustomerById(int custId)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.CustomerID.Equals(custId)).SingleOrDefault();
            if (cust != null) //found
            {
                CustomerInfo c = new CustomerInfo();
                c.CustomerID = cust.CustomerID;
                c.Firstname = cust.Firstname;
                c.Lastname = cust.Lastname;
                c.PersonalIdentity = cust.PersonalIdentity;
                c.Account = cust.Account;
                c.Password = cust.Password;
                c.Job = cust.Job;
                c.Dob = cust.Dob;
                c.Address = cust.Address;
                c.Phone = cust.Phone;
                c.Email = cust.Email;

                return c;
            }
            else // not found
            {
                return null;
            }
        }

        public Boolean ConfirmPassword(int custID, string password)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.CustomerID.Equals(custID) && c.Password.Equals(password)).SingleOrDefault();
            if (cust != null) //found
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void UpdateInfomation(CustomerInfo customer)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.CustomerID == customer.CustomerID).SingleOrDefault();
            if (cust != null) // found
            {
                cust.Firstname = customer.Firstname;
                cust.Lastname = customer.Lastname;
                cust.Job = customer.Job;
                cust.Dob = customer.Dob;
                cust.Address = customer.Address;
                cust.Phone = customer.Phone;
                cust.Email = customer.Email;

                db.SubmitChanges();
            }

        }

        public Boolean UpdatePassword(int custID, string newPass)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var cust = db.Customers.Where(c => c.CustomerID.Equals(custID)).SingleOrDefault();
            if (cust != null)
            {
                cust.Password = newPass;
                db.SubmitChanges();
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
